import { _decorator,Animation, Component, find, instantiate, Node, Prefab } from 'cc';
import { GameManager, GameState } from '../GameManager';
const { ccclass, property } = _decorator;

@ccclass('Ships_Frist')
export class Ships_Frist extends Component {
    
    @property(Prefab)
    bullet_1:Prefab=null;
    
    @property
    speed: number = 300;
    @property
    hp: number = 1;
    
    private lastBulletTime: number = 0; // 上一次生成子弹的时间
    
    @property(Animation)
    anim:Animation = null;
    start() {

    }

    update(deltaTime: number) {
        const currentTime = Date.now();
        if (currentTime - this.lastBulletTime >= 200) {        //每隔1000毫秒调用
            this.lastBulletTime = currentTime;

            if(GameManager.getInstance().gameState==GameState.START){
                this.attack();
            }
        }
        
    }
    attack() {
        if(this.bullet_1){
            const newBullet = instantiate(this.bullet_1);
            const PlayerBullets=find("Canvas/BG/Bullets");
            PlayerBullets.addChild(newBullet);

            const newPos=this.node.getWorldPosition()
            newPos.y+=50;
            newBullet.setWorldPosition(newPos);
        }

    }
}


